package com.huanglj.taobaounion.ui.fragment;

import android.content.Intent;
import android.view.View;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.huanglj.taobaounion.R;
import com.huanglj.taobaounion.base.BaseFragment;
import com.huanglj.taobaounion.model.domain.SelectedByCategory;
import com.huanglj.taobaounion.model.domain.SelectedPage;
import com.huanglj.taobaounion.presenter.ISelectedPagePresenter;
import com.huanglj.taobaounion.ui.activity.TicketActivity;
import com.huanglj.taobaounion.ui.adapter.SelectedPageLeftAdapter;
import com.huanglj.taobaounion.ui.adapter.SelectedPageRightAdapter;
import com.huanglj.taobaounion.utils.LogUtil;
import com.huanglj.taobaounion.utils.PresenterManager;
import com.huanglj.taobaounion.view.ISelectedPageCallBack;

import java.util.List;

import butterknife.BindView;

import static com.huanglj.taobaounion.utils.Contants.SELECT_INIT;
import static com.huanglj.taobaounion.utils.Contants.SELECT_LEFT;
import static com.huanglj.taobaounion.utils.Contants.SELECT_RIGHT;
import static com.huanglj.taobaounion.utils.Contants.SELECT_RIGHT_COUNT;


public class SelectedFragment extends BaseFragment implements ISelectedPageCallBack {

    private static final String TAG = "SelectedFragment";
    @BindView(R.id.left_type_list)
    RecyclerView mLeftRecyclerView;
    @BindView(R.id.right_content_list)
    RecyclerView mRightRecyclerView;
    private SelectedPageLeftAdapter mSelectedPageLeftAdapter;
    private ISelectedPagePresenter mSelectPagePresenter;
    private SelectedPage.DataBean mDataBean;
    private SelectedPageRightAdapter mSelectedPageRightAdapter;
    private int selectNetState = 0;
    private SelectedPage.DataBean mFirstData;
    private SelectedPage.DataBean mContent;


    @Override
    public int getRes() {

        return R.layout.fragment_selected;
    }

    @Override
    protected void initPresenter() {
        mSelectPagePresenter = PresenterManager.getInstance().getSelectPagePresenter();
        mSelectPagePresenter.registerViewCallBack(this);
        selectNetState = SELECT_INIT;
        mSelectPagePresenter.getCategories();
    }

    @Override
    protected void loadData() {

    }

    @Override
    protected void initListener() {
        mSelectedPageLeftAdapter.setOnItemClickListener(item -> {
            selectNetState = SELECT_RIGHT;
            mContent = item;
            mSelectPagePresenter.getContentByCategories(item);
        });
        mSelectedPageRightAdapter.setonButtonClickListener((title, url, pickCoverUrl) -> {
            Intent intent = new Intent(SelectedFragment.this.getContext(), TicketActivity.class);
            PresenterManager.getInstance().getTicketPresenter().getTicket(title,url,pickCoverUrl);
            SelectedFragment.this.startActivity(intent);
        });
    }

    @Override
    public void initView(View view) {
        //setUpState(State.SUCCESS);
        mLeftRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mSelectedPageLeftAdapter = new SelectedPageLeftAdapter();
        mLeftRecyclerView.setAdapter(mSelectedPageLeftAdapter);

        mRightRecyclerView.setLayoutManager(new GridLayoutManager(getContext(), SELECT_RIGHT_COUNT));
        mSelectedPageRightAdapter = new SelectedPageRightAdapter();
        mRightRecyclerView.setAdapter(mSelectedPageRightAdapter);
}

    @Override
    public void onCategoriesLoaded(SelectedPage result) {
        setUpState(State.SUCCESS);
        mSelectedPageLeftAdapter.setData(result.getData());
        //初始化的时候加载加载第一个内容
        selectNetState = SELECT_LEFT;
        mFirstData = result.getData().get(0);
        mSelectPagePresenter.getContentByCategories(mFirstData);
    }

    @Override
    public void onCategoryById(List<SelectedByCategory.DataBean.TbkDgOptimusMaterialResponseBean.ResultListBean.MapDataBean> result){
        setUpState(State.SUCCESS);
        mSelectedPageRightAdapter.setData(result);
    }

    @Override
    public void onLoading() {
        setUpState(State.LOADING);
    }

    @Override
    public void onEmpty() {
        setUpState(State.ERROR);

    }

    @Override
    public void onError(String errorMsg) {
        setUpState(State.ERROR);

    }

    @Override
    protected void onRetryClick() {
        switch (selectNetState) {
            case SELECT_INIT:
                mSelectPagePresenter.getCategories();
                break;
            case SELECT_LEFT:
                mSelectPagePresenter.getContentByCategories(mFirstData);
                break;
            case SELECT_RIGHT:
                mSelectPagePresenter.getContentByCategories(mContent);
                break;
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mSelectPagePresenter.unregisterViewCallBack(this);
    }
}
